// pages/admin/login/login.js
const app = getApp()
var host = app.globalData.host;
var cryptoJS = require('../../../utils/crypto-js');
var self = null;

Page({
  data: {
    themeColor: '',
    shop: '',
    account: '',
    mobile: '',
    password: ''
  },
  onLoad: function (options) {
    self = this;
    self.getStyle();
  },
  getStyle() {
    self.setData({
      themeColor: app.globalData.themeColor
    })
  },
  onShopInput(e) {
    self.setData({
      shop: e.detail.value
    })
  },
  onAccountInput(e) {
    self.setData({
      account: e.detail.value
    })
  },
  onMobileInput(e) {
    self.setData({
      mobile: e.detail.value
    })
  },
  onPasswordInput(e) {
    self.setData({
      password: e.detail.value
    })
  },
  login() {
    if (!self.data.account || !self.data.password) {
      wx.showToast({
        title: '账号/密码不能为空',
        icon: 'none'
      })
      return;
    }
    var url = host + '/yq/adminShop/shop/auth';
    var key = cryptoJS.enc.Utf8.parse(app.globalData.aesKey);
    var iv = cryptoJS.enc.Utf8.parse('RandomInitVector');
    var aesText = cryptoJS.AES.encrypt(self.data.password, key, {
      iv: iv,
      mode: cryptoJS.mode.CBC,
      padding: cryptoJS.pad.Pkcs7
    }).toString();

    var params = {
      // shop: self.data.shop,
      // mobile: self.data.mobile,
      account: self.data.account,
      password: aesText
    };
    var success = function (res) {
      app.globalData.shopUser = res.info;
      wx.setStorageSync("shopUser", res.info);
      wx.reLaunch({
        url: '/pages/admin/scan/scan'
      })
    }
    var fail = function (res) {
      console.log(res);
    };
    var complete = function () { };
    app.requestLoading(url, params, success, fail, complete);
  }
})
